unit CustomerLoanPaymentForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uADStanIntf, uADStanOption, UDF,
  uADStanParam, uADStanError, uADDatSManager, uADPhysIntf, uADDAptIntf,
  uADStanAsync, uADDAptManager, uADGUIxIntf, uADGUIxFormsWait, uADStanDef,
  uADStanPool, uADPhysManager, Vcl.DBCtrls, Vcl.ExtCtrls, Vcl.StdCtrls,
  Vcl.Grids, Vcl.DBGrids, Data.DB, uADCompClient, uADCompGUIx, uADPhysMySQL,
  uADPhysIB, uADCompDataSet, System.UITypes;

type
  TfmCustomerLoanPayment = class(TForm)
    qrySQL: TADQuery;
    dsLoanSettings: TDataSource;
    adtLoanSettings: TADTable;
    adtCustomers: TADTable;
    dsCustomers: TDataSource;
    ADPhysIBDriverLink1: TADPhysIBDriverLink;
    ADPhysMySQLDriverLink1: TADPhysMySQLDriverLink;
    ADGUIxWaitCursor1: TADGUIxWaitCursor;
    ADConnection1: TADConnection;
    Label13: TLabel;
    DBGrid1: TDBGrid;
    btnClose: TButton;
    btnSave: TButton;
    btnDelete: TButton;
    btnEdit: TButton;
    btnNew: TButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    DBText1: TDBText;
    DBText2: TDBText;
    DBText3: TDBText;
    DBText4: TDBText;
    DBText5: TDBText;
    DBText6: TDBText;
    DBText7: TDBText;
    DBText8: TDBText;
    DBText9: TDBText;
    DBGrid2: TDBGrid;
    adtCustomerLoanPayment: TADTable;
    dsCustomerLoanPayment: TDataSource;
    DBNavigator1: TDBNavigator;
    procedure FormCreate(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmCustomerLoanPayment: TfmCustomerLoanPayment;

implementation

{$R *.dfm}

procedure TfmCustomerLoanPayment.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfmCustomerLoanPayment.DBNavigator1Click(Sender: TObject;
  Button: TNavigateBtn);
var
  InstallmentNo: integer;
  Payment: Variant;
begin
  if Button = nbInsert then
  begin
    InstallmentNo := adtCustomerLoanPayment.RecordCount;
    if InstallmentNo < adtLoanSettings.FieldByName('INSTALLMENTS').Value then
    begin
      Inc(InstallmentNo);
      adtCustomerLoanPayment.FieldByName('INSTALLMENT_NO').AsInteger := InstallmentNo;
      adtCustomerLoanPayment.FieldByName('CUSTOMER_CODE').Value := adtLoanSettings.FieldByName('CUSTOMER_CODE').Value;
      adtCustomerLoanPayment.FieldByName('LOANSETTING_ID').Value := adtLoanSettings.FieldByName('ID').Value;
      adtCustomerLoanPayment.FieldByName('CREATEDATE').AsDateTime := Now;
      //check whether is last payment, if yes, use LAST_PERIOD_PAYMENT, if no use PAYMENT_PER_INSTALLMENT;
      if InstallmentNo = adtLoanSettings.FieldByName('INSTALLMENTS').Value then
        Payment := adtLoanSettings.FieldByName('LAST_PERIOD_PAYMENT').Value
      else
        Payment := adtLoanSettings.FieldByName('PAYMENT_PER_INSTALLMENT').Value;
      adtCustomerLoanPayment.FieldByName('PAYMENT').Value := Payment;
      adtCustomerLoanPayment.FieldByName('PAID_AMOUNT').Value := Payment;
      //update loansettings STATUS field to FULL PAID, ACTIVE to N
    end else begin
      ShowMessage('Full paid already!');
    end;
  end;
end;

procedure TfmCustomerLoanPayment.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
var
  mdResult: integer;
begin
  if adtCustomerLoanPayment.Modified then
    mdResult := MessageDlg('Loan payment has record pending update, commit?', mtWarning, [mbYes, mbNo, mbCancel], 0)
  else
    mdResult := mrYes;
  if mdResult = mrCancel then Exit;
  CanClose := mdResult = mrYes;
  if CanClose then
    FreeAndNil(fmCustomerLoanPayment);
end;

procedure TfmCustomerLoanPayment.FormCreate(Sender: TObject);
var
  ADPhysDriverLink: TADPhysDriverLink;
  DriverID: string;
begin
  DriverID := DBDriverID;
  if DriverID = 'Firebird' then
    ADPhysDriverLink := ADPhysIBDriverLink1;
  if DriverID = 'MySQL' then
    ADPhysDriverLink := ADPhysMySQLDriverLink1;
  SetDBInfo(ADConnection1, ADPhysDriverLink);
  adtCustomers.Active := True;
  adtLoanSettings.Active := True;
  adtCustomerLoanPayment.Active := True;
end;

end.
